﻿using Microsoft.AspNetCore.Authorization;
using Ocelot.Admin.Api.Core;

namespace Ocelot.Admin.Api.Application
{
    /// <summary>
    /// 系统服务接口
    /// </summary>
    public class InitApiController : IDynamicApiController
    {
        private SqlSugarScopeProvider _sqlserverDB;
        private AppInfoOptions _appInfoOptions;

        public InitApiController(ISqlSugarClient db, IOptionsMonitor<AppInfoOptions> optionsMonitor)
        {
            _sqlserverDB = db.AsTenant().GetConnectionScope(DatabaseConfigIdConst.OcelotSettings);
            _appInfoOptions = optionsMonitor.CurrentValue;
        }

        [AllowAnonymous]
        public async Task<bool> TableAsync()
        {
            var b = _sqlserverDB.DbMaintenance.CreateDatabase();
            _sqlserverDB.CodeFirst.InitTables(typeof(UserAccount),
                typeof(Dictionary),
                typeof(DictionaryItem),
                typeof(GlobalConfiguration),
                typeof(Project),
                typeof(Route),
                typeof(RouteHostPort),
                typeof(RouteProperty),
                typeof(SettingBak));
            await _sqlserverDB.Storageable<UserAccount>(new UserAccount() { Account = "admin", Password = "123456", Enabled = true, Name = "admin" ,OpenId=""}).ExecuteCommandAsync();
            await _sqlserverDB.Storageable<Dictionary>(new Dictionary() { DictionaryName = "consul配置关键字", DictionaryType = "ConsulSettingKey" }).ExecuteCommandAsync();
            await _sqlserverDB.Storageable<Dictionary>(new Dictionary() { DictionaryName = "consul DC 库", DictionaryType = "ConsulDC" }).ExecuteCommandAsync();
            return b;
        }
    }
}
